package org.pnp.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class StockDBAdapter {
	// Database fields
	public static final String KEY_ROWID = "_id";
	public static final String KEY_STOCKCODE = "Stock_Code";
	public static final String KEY_STOCKNAME = "Stock_Name";
	public static final String KEY_YTDCLOSEDPRICE = "YTD_Closed_Price";
	public static final String KEY_UPWARNPRICE = "Up_Warn_Price";
	public static final String KEY_DOWNWARNPRICE = "Down_Warn_Price";
	private static final String DATABASE_TABLE = "stockInfo";
	private Context context;
	private SQLiteDatabase database;
	private StockDatabaseHelper dbHelper;

	public StockDBAdapter(Context context) {
		this.context = context;
	}

	public StockDBAdapter open() throws SQLException {
		dbHelper = new StockDatabaseHelper(context);
		//when call .getWritableDatabase() method first time, method onCreate in StockDatabaseHelper will be called to create the DB
		database = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	/**
	 * Create a new Stock If the Stock is successfully created return the new
	 * rowId for that note, otherwise return a -1 to indicate failure.
	 */
	public long createStock(String stockCode, String stockName, double ytdClosedPrice, double upWarnPrice,
			double downWarnPrice) {
		ContentValues initialValues = createContentValues( stockCode, stockName, ytdClosedPrice, upWarnPrice,
				 downWarnPrice);

		return database.insert(DATABASE_TABLE, null, initialValues);
	}

	/**
	 * Update the Stock
	 */
	public boolean updateStock(long rowId, String stockCode, String stockName, double ytdClosedPrice, double upWarnPrice,
			double downWarnPrice) {
		ContentValues updateValues = createContentValues( stockCode, stockName, ytdClosedPrice, upWarnPrice,
				downWarnPrice);

		return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "="
				+ rowId, null) > 0;
	}

	/**
	 * Deletes Stock
	 */
	public boolean deleteStock(long rowId) {
		return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	/**
	 * Return a Cursor over the list of all Stock in the database
	 * 
	 * @return Cursor over all notes
	 */
	public Cursor fetchAllStocks() {
		return database.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_STOCKCODE,
				KEY_STOCKNAME, KEY_YTDCLOSEDPRICE, KEY_UPWARNPRICE, KEY_DOWNWARNPRICE }, null, null, null,
				null, null);
	}

	/**
	 * Return a Cursor positioned at the defined Stock
	 */
	public Cursor fetchStock(long rowId) throws SQLException {
		Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
				KEY_ROWID, KEY_STOCKCODE, KEY_STOCKNAME, KEY_YTDCLOSEDPRICE, KEY_UPWARNPRICE, KEY_DOWNWARNPRICE },
				KEY_ROWID + "=" + rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	private ContentValues createContentValues(String stockCode, String stockName, double ytdClosedPrice, double upWarnPrice,
			double downWarnPrice) {
		ContentValues values = new ContentValues();
		values.put(KEY_STOCKCODE, stockCode);
		values.put(KEY_STOCKNAME, stockName);
		values.put(KEY_YTDCLOSEDPRICE, ytdClosedPrice);
		values.put(KEY_UPWARNPRICE, upWarnPrice);
		values.put(KEY_DOWNWARNPRICE,downWarnPrice);
		return values;
	}


}
